using IQIGame.Onigao.Framework;
using NetProtocol.POD;
using System.Collections.Generic;
using UnityEngine.Assertions;

namespace IQIGame.Onigao.GamePlay
{
    /// <summary>
    /// 关卡Player数据
    /// </summary>
    public partial class DynLevelPlayer
    {
        /// <summary>
        /// playerId
        /// </summary>
        public long id { get; private set; }
        /// <summary>
        /// player name
        /// </summary>
        public string name { get; private set; }
        /// <summary>
        /// 玩家等级
        /// </summary>
        public int level { get; private set; }
        /// <summary>
        /// 玩家形象（头像图标）配置
        /// </summary>
        public int avatar { get; private set; }
        /// <summary>
        /// 玩家头像框图片配置
        /// </summary>
        public int avatarFrame { get; private set; }

        public BetterList<long, DynLevelHero> levelHeros { protected set; get; } = new BetterList<long, DynLevelHero>();

        /// public List<DynLevelHero> heroLists { protected set; get; } = new List<DynLevelHero>();

        public virtual long leaderHeroId { set; get; }

        public int regionId { protected set; get; }

        public int followerId { get; private set; }

        public int customCameraCid { get; set; }
        public bool isQuickBarDisabled { get; private set; }
        public int manualTrackingEntity { get; set; }

        public LevelBuffHolder buffHolder { get; protected set; }

        public BetterList<int, LevelBuffData> buffs => buffHolder.buffs;

        private BattleSettlementData _latestBattleSettlementData;

        protected DynLevelPlayer()
        {
        }

        protected virtual void CreateHerosByPOD(List<LevelHeroPOD> Heroes, long LeaderHeroId)
        {
            levelHeros.Clear();
            foreach (var h in Heroes)
            {
                var levelHero = DynLevelHero.CreateByPOD(h);
                levelHeros.Add(h.id, levelHero);
            }
            Assert.IsTrue(levelHeros.ContainsKey(LeaderHeroId), $"Leader Hero id not found in team,id:{LeaderHeroId}");
            this.leaderHeroId = LeaderHeroId;
        }


        public static DynLevelPlayer CreateByPOD(LevelPlayerPOD pod, int regionCid)
        {
            var player = new DynLevelPlayer();
            player.id = pod.id;
            player.name = pod.name;
            player.level = pod.level;
            player.avatar = pod.avatar;
            player.avatarFrame = pod.avatarFrame;
            player.CreateHerosByPOD(pod.heroes, pod.leaderHeroId);
            player.regionId = regionCid;
            player.followerId = pod.followerId;
            player.customCameraCid = pod.customCameraCid;
            player.isQuickBarDisabled = pod.isQuickBarDisabled;
            player.buffHolder = new LevelBuffHolder(LevelBuffHostType.Player, player.id);
            player.buffHolder.Init(pod.buffs);
            return player;
        }

        public static DynLevelPlayer CreateByPOD(LevelOtherPlayerPOD pod, int regionCid)
        {
            var player = new DynLevelPlayer();
            player.id = pod.id;
            player.name = pod.name;
            player.level = pod.level;
            player.avatar = pod.avatar;
            player.avatarFrame = pod.avatarFrame;
            player.CreateHerosByPOD(pod.heroes, pod.leaderHeroId);
            player.regionId = regionCid;
            return player;
        }

        /// <summary>
        /// 获取队长英雄数据
        /// </summary>
        /// <returns></returns>
        public DynLevelHero GetLeaderHero()
        {
            levelHeros.TryGetValue(this.leaderHeroId, out var hero);
            return hero;
        }

        public void ChangeFollower(int id)
        {
            followerId = id;
        }

        /// <summary>
        /// 战斗结束后添加数据，以便玩家随时点击查看（策划需求战斗结束不弹结算面板，除非玩家手动点开）
        /// </summary>
        /// <param name="data"></param>
        public void AddBattleSettlementData(BattleSettlementData data)
        {
            _latestBattleSettlementData = data;
        }

        /// <summary>
        /// 通过战斗结算统计数据更新hp和sp
        /// 其他更新使用重载方法
        /// </summary>
        /// <param name="units"></param>
        public void UpdateHeroAttrBattleChange(List<BattleStatisticsUnitPOD> units)
        {
            foreach (var pod in units)
            {
                foreach (var dynHero in levelHeros)
                {
                    if (dynHero.cid == pod.cid)
                    {
                        dynHero.RefreshHpSp(pod.hp, pod.sp);
                        break;
                    }
                }
            }
            MsgDispatcher.Broadcast(MsgEventType.Level_UpdateHeroHpSp);
        }

        public BattleSettlementData ObtainBattleSettlementData()
        {
            BattleSettlementData data = _latestBattleSettlementData;
            _latestBattleSettlementData = null;
            return data;
        }

        public void OnRevive()
        {
            //清理buff（不用服务器专门通知一次了，这是固定操作，buff同步的消息很复杂，没必要）
            buffHolder.Clear();
            foreach (var dynHero in levelHeros)
            {
                dynHero.OnRevive();
            }
        }
    }
}
